package com.example.demo.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @Date 14:29 2018/05/29
 * @Description  错误日志工具
 */
public class ErrorLogsUtil {
 
    /**
     * @param: [e]
     * @return  void
     * @Description  打印错误日志并保存到数据库
     */
    public static void error(Exception e) {
        StackTraceElement stackTraceElement= e.getStackTrace()[0];
        Connection con = null;
        try {
        	con = ConnectionFactory.getDatabaseConnection();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        PreparedStatement ps = null;
        String sql = "INSERT INTO itc_error_logs VALUES (UUID(), NOW(), ?, ?)";
        try {
            ps = con.prepareStatement(sql);
            //打印日志，错在第几行
            String errorInfo = e.toString()+",errorMassage:"+stackTraceElement+","+"errorLine:"+stackTraceElement.getLineNumber();
            ps.setString(1, errorInfo);
            ps.execute();
        } catch (SQLException e1) {
            e1.printStackTrace();
        } finally {
            try {
                if(ps != null) {
                    ps.close();
                }
                if(con != null) {
                    con.close();
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
 
        }
    }
}
